# coding=utf-8
import time
from function_utils import Global, Human, Common, PsUtil, TableRender


def network_info(interval=2):
    info = 'Device|Receive|Send|Receive/s|Send/s'
    last = PsUtil.net_io()
    time.sleep(interval)
    curr = PsUtil.net_io()
    for dev, nets in curr.items():
        last_nets = last[dev]
        recv = nets['bytes_recv']
        send = nets['bytes_sent']
        info = '%s\n%s|%s|%s|%s/s|%s/s' % (info, dev, Human.size(recv), Human.size(send),
                                     Human.size((recv - last_nets['bytes_recv']) / interval),
                                     Human.size((send - last_nets['bytes_sent']) / interval)
                                     )
    info = '''
<BLUE> ##### > Network IO:
%s

<BLUE> ##### > IP Address:
%s
    ''' % (TableRender(info), Common.execute_cmd('ip a')[1])
    return info


def write_widgets_value(value):
    data = [
        {"MultiInfoText": value},
        {},
        {}
    ]
    Global.values_write(data)
    Global.report_info(100)


def main():
    write_widgets_value(network_info())


if __name__ == '__main__':
    main()
